package com.stationbuilding.fragment;

import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.VideoView;
import com.bumptech.glide.Glide;
import com.danikula.videocache.HttpProxyCacheServer;
import com.stationbuilding.MyApplication;
import com.stationbuilding.R;
import com.stationbuilding.activity.safe.SafeAddActivity;
import com.stationbuilding.constants.Constants;

public class ImageFragment extends Fragment implements View.OnClickListener {

    private ImageView ivVideo;
    private ImageView ivImage;
    private VideoView videoView;
    private int type;
    private String path;
    private ProgressDialog dialog;//加载提示

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setRetainInstance(true);
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_image, container, false);
        view.setOnClickListener(this);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view,savedInstanceState);
        type = getArguments().getInt("type");
        path = getArguments().getString("path");
        ivImage = (ImageView) view.findViewById(R.id.image);
        ivVideo = (ImageView) view.findViewById(R.id.video_image);
        ivVideo.setOnClickListener(this);
        videoView = (VideoView) view.findViewById(R.id.video);
        if (type == Constants.PHOTO) {
            ivVideo.setVisibility(View.GONE);
            Glide.with(getActivity()).load(path).into(ivImage);
        } else {
            ivVideo.setVisibility(View.VISIBLE);
            Glide.with(getActivity()).load(path.split(",")[0]).into(ivImage);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        videoView.setVisibility(View.GONE);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.video_image) {
            dialog = ProgressDialog.show(getActivity(), null, "视频缓冲中...");
            ivVideo.setVisibility(View.GONE);
            ivImage.setVisibility(View.GONE);
            videoView.setVisibility(View.VISIBLE);
            //设置视频控制器
            videoView.setMediaController(new MediaController(getActivity()));
            //设置视频路径
            if (path.startsWith(Constants.BASE_URL)) {
                HttpProxyCacheServer proxy = MyApplication.getProxy();
                String proxyUrl = proxy.getProxyUrl(Constants.BASE_URL + path.split(",")[1]);//传入视频的url
                videoView.setVideoPath(proxyUrl);
            } else {
                videoView.setVideoPath(path);
            }
            videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    dialog.dismiss();
                    videoView.start();//开始播放视频
                }
            });
            videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    videoView.setVisibility(View.GONE);
                    ivVideo.setVisibility(View.VISIBLE);
                    ivImage.setVisibility(View.VISIBLE);
                }
            });
        } else if (v.getId() == R.id.image_fragment) {
            mOnItemClickListener.onItemClickListener();
        }
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener();
    }
}
